CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
IF(COMMAND CMAKE_POLICY)
  CMAKE_POLICY(SET CMP0003 NEW)
ENDIF(COMMAND CMAKE_POLICY)

PROJECT( IGSTK )

# version
SET(IGSTK_VERSION_MAJOR "5")
SET(IGSTK_VERSION_MINOR "2")
SET(IGSTK_VERSION_PATCH "0")

SET (LIBRARY_OUTPUT_PATH ${IGSTK_BINARY_DIR}/bin CACHE PATH "Single output directory for building all libraries." FORCE)
SET (EXECUTABLE_OUTPUT_PATH ${IGSTK_BINARY_DIR}/bin CACHE PATH "Single output directory for building all executables." FORCE)

# Append the library version information to the library target properties.
OPTION(IGSTK_WITH_LIBRARY_VERSION "Build with library version information" OFF)
IF(IGSTK_WITH_LIBRARY_VERSION)
  SET(IGSTK_LIBRARY_PROPERTIES ${IGSTK_LIBRARY_PROPERTIES}
#   BUILD_WITH_INSTALL_RPATH OFF
#   INSTALL_RPATH ${CMAKE_INSTALL_PREFIX}/lib
#   INSTALL_NAME_DIR ${CMAKE_INSTALL_PREFIX}/lib
    VERSION ${IGSTK_VERSION_MAJOR}.${IGSTK_VERSION_MINOR}.${IGSTK_VERSION_PATCH}
    SOVERSION ${IGSTK_VERSION_MAJOR}
  )
ENDIF(IGSTK_WITH_LIBRARY_VERSION)

# Build with shared libraries
OPTION(BUILD_SHARED_LIBS "Build with shared libraries" OFF)

ENABLE_TESTING()
INCLUDE (${CMAKE_ROOT}/Modules/Dart.cmake)

SET(CXX_TEST_PATH ${EXECUTABLE_OUTPUT_PATH})

#
#  FIND ITK
#
find_package(ITK REQUIRED)
set( ITK_MIN_VERSION 4.2.0 )

set( ITK_VERSION ${ITK_VERSION_MAJOR}.${ITK_VERSION_MINOR}.${ITK_VERSION_PATCH} )

if( ${ITK_VERSION} VERSION_LESS ${ITK_MIN_VERSION} )
  message( FATAL_ERROR "This application requires ITK version ${ITK_MIN_VERSION} or later. Provided version is ${ITK_VERSION}.")
else()
  INCLUDE (${USE_ITK_FILE})
endif()

#
# FIND VTK
#
find_package(VTK REQUIRED)
set( VTK_MIN_VERSION 5.10.0 )

set( VTK_VERSION ${VTK_VERSION_MAJOR}.${VTK_VERSION_MINOR}.${VTK_VERSION_PATCH} )

if( ${VTK_VERSION} VERSION_LESS ${VTK_MIN_VERSION} )
  message( FATAL_ERROR "This application requires VTK version ${VTK_MIN_VERSION} or later. Provided version is ${VTK_VERSION}.")
else()
  INCLUDE (${USE_VTK_FILE})
endif()

#
# FIND FLTK
#
OPTION(IGSTK_USE_FLTK "Use FLTK (The Fast Light Toolkit) for GUI" OFF)
IF(IGSTK_USE_FLTK)
  find_package(FLTK)
  IF (FLTK_FOUND)
    INCLUDE_DIRECTORIES (${FLTK_INCLUDE_DIR})
    IF(WIN32)
      ADD_DEFINITIONS(-DWIN32)
    ENDIF(WIN32)
    LINK_LIBRARIES( ${FLTK_LIBRARIES} ${OPENGL_LIBRARIES} )
    IF(CYGWIN)
      LINK_LIBRARIES( ole32 uuid comctl32 wsock32 supc++ -lm -lgdi32)
    ENDIF(CYGWIN)
  ELSE (FLTK_FOUND)
    MESSAGE( FATAL_ERROR "This application requires FLTK. One of these components is missing. Please verify configuration")
  ENDIF (FLTK_FOUND)
ENDIF(IGSTK_USE_FLTK)

#
# FIND Qt
#
OPTION(IGSTK_USE_Qt "Use Qt for GUI" OFF)
IF(IGSTK_USE_Qt)
 # Check if VTK was built with GUISupport on
 IF( NOT VTK_USE_GUISUPPORT )
  MESSAGE(FATAL_ERROR "To build IGSTK with Qt support, you will need to use a VTK
build with GUI support i.e VTK_USE_GUISUPPORT turned on in VTK configuration")
 ENDIF( NOT VTK_USE_GUISUPPORT )

 # Check if VTK was built with Qt on
 IF( NOT VTK_USE_QVTK )
  MESSAGE(FATAL_ERROR "To build IGSTK with Qt support, you will need to use a VTK
build with Qt support i.e VTK_USE_QVTK turned on in VTK configuration")
 ENDIF( NOT VTK_USE_QVTK )

 # use what QVTK built with
 SET(QT_QMAKE_EXECUTABLE ${VTK_QT_QMAKE_EXECUTABLE} CACHE FILEPATH "")
 SET(QT_MOC_EXECUTABLE ${VTK_QT_MOC_EXECUTABLE} CACHE FILEPATH "")
 SET(QT_UIC_EXECUTABLE ${VTK_QT_UIC_EXECUTABLE} CACHE FILEPATH "")
 SET(DESIRED_QT_VERSION ${VTK_DESIRED_QT_VERSION} CACHE STRING "")

 IF( DESIRED_QT_VERSION MATCHES 3 )
    MESSAGE( FATAL_ERROR " VTK built using Qt 3. Please use Qt4" )
 ENDIF( DESIRED_QT_VERSION MATCHES 3)

 find_package(Qt4 REQUIRED)

 IF(QT_USE_FILE)
   INCLUDE(${QT_USE_FILE})
 ELSE(QT_USE_FILE)
   SET(QT_LIBRARIES   ${QT_QT_LIBRARY})
 ENDIF(QT_USE_FILE)

 INCLUDE_DIRECTORIES (${QT_INCLUDE_DIR})

ENDIF(IGSTK_USE_Qt)

# The IGSTK Library
SUBDIRS(Source)

# Add the testing directories
IF(BUILD_TESTING)
   SUBDIRS(Testing)
ENDIF(BUILD_TESTING)

# Add the examples directories
OPTION(BUILD_EXAMPLES "Examples of the project" ON)
IF(BUILD_EXAMPLES)
   SUBDIRS(Examples)
ENDIF(BUILD_EXAMPLES)

#
#User wants to use the Micron tracker. We force the user to
#manually select the directory containing the Micron tracker header
#file and the library. This way there is no chance that our
#automatic selection chooses a 32 bit distribution when the rest of
#the user code is 64bit. The installer from Claron installs both 32
#and 64 and if the Cmake code doesn't select the correct version
#the user will have a tedious debug session until realizing the
#cause of the problem. We choose to force the user to explicitly
#set two variables during Cmake configuration vs. several hours of
#debugging code that has no bugs.
#
OPTION(IGSTK_USE_MicronTracker "Enable the use of the Micron tracker" OFF)

if(IGSTK_USE_MicronTracker)

  set(MTC_INCLUDE_DIRECTORY "" CACHE PATH "Directory where the MTC.h file is located")
  if(MTC_INCLUDE_DIRECTORY STREQUAL "")
    message( FATAL_ERROR "Please set the MTC_INCLUDE_DIRECTORY variable value.")
  else()
    include(${IGSTK_SOURCE_DIR}/CMake/ExtractMTCVersion.cmake)
    if( ${MTCMajorVersion}.${MTCMinorVersion} LESS 3.6.1 )
      message( FATAL_ERROR " Your MTC version is ${MTCMajorVersion}.${MTCMinorVersion}. Version 3.6.1 or later should be used")
    endif()
    include_directories( ${MTC_INCLUDE_DIRECTORY} )
    include_directories( ${IGSTK_SOURCE_DIR}/Utilities/MicronTracker/src )
  endif()


  set(MTC_LIBRARY "" CACHE FILEPATH "Full path to the MTC library (e.g. on win MTC.lib, on linux libMTC.a)")
  if(MTC_LIBRARY STREQUAL "")
    message( FATAL_ERROR "Please set the MTC_LIBRARY variable value.")
  else()
    get_filename_component( MTC_LIBRARY_DIRS ${MTC_LIBRARY} PATH )
    link_directories( ${MTC_LIBRARY_DIRS} )
  endif()

endif()

# Connect InfiniTrack
OPTION(IGSTK_USE_InfiniTrack "Connect InfiniTrack " OFF)

IF(IGSTK_USE_InfiniTrack)
  IF(WIN32)
    FIND_PATH(InfiniTrack_INCLUDE_DIRECTORY itkInterface.h
    "C:/Program Files/Atracsys/infiniTrack/include"
    DOC "Directory where the itkInterface.h file is located")
    INCLUDE_DIRECTORIES( ${InfiniTrack_INCLUDE_DIRECTORY} )

    FIND_LIBRARY( InfiniTrack_LIBRARY
      NAMES v9ITK.dll v9ITK
      PATHS "C:/Program Files/Atracsys/infiniTrack/lib"
      DOC "Full path to the InfiniTrack library file"
      )

    IF(InfiniTrack_INCLUDE_DIRECTORY)
      GET_FILENAME_COMPONENT( InfiniTrack_LIBRARY_DIRS ${InfiniTrack_LIBRARY} PATH )
      LINK_DIRECTORIES( ${InfiniTrack_LIBRARY_DIRS} )
    ENDIF(InfiniTrack_INCLUDE_DIRECTORY)

  ELSE(WIN32)
    MESSAGE(FATAL_ERROR " Please turn off InfiniTrack option. Only windows operating system is currently supported for InfiniTrack")
  ENDIF(WIN32)

ENDIF(IGSTK_USE_InfiniTrack)

# Connect Ascension 3DG tracker
OPTION(IGSTK_USE_Ascension3DGTracker "Connect Ascension 3D Guidance Tracker" OFF)
IF(IGSTK_USE_Ascension3DGTracker)
  OPTION(Ascension3DG_MedSafe "Use MedSafe version of the tracker" OFF)
  IF(Ascension3DG_MedSafe) # Use MedSafe version
    FIND_PATH(ATC_INCLUDE_DIRECTORY ATC3DGm.h
    "C:/Program Files/Ascension/3D Guidance medSAFE (Rev F)/3D Guidance API"
    DOC "Directory where the ATC3DGm.h file is located")
    INCLUDE_DIRECTORIES( ${ATC_INCLUDE_DIRECTORY} )

    FIND_LIBRARY( ATC_LIBRARY
      NAMES ATC3DGm ATC3DGm.dll
      PATHS "C:/Program Files/Ascension/3D Guidance medSAFE (Rev F)/3D Guidance API"
      DOC "Full path to the ATC3DG.dll file"
      )
  ELSE(Ascension3DG_MedSafe) # Use non MedSafe version
    FIND_PATH(ATC_INCLUDE_DIRECTORY ATC3DG.h
    "C:/Program Files/Ascension/3D Guidance (Rev D)/3D Guidance API"
    DOC "Directory where the ATC3DG.h file is located")
    INCLUDE_DIRECTORIES( ${ATC_INCLUDE_DIRECTORY} )

    FIND_LIBRARY( ATC_LIBRARY
      NAMES ATC3DG ATC3DG.dll
      PATHS "C:/Program Files/Ascension/3D Guidance (Rev D)/3D Guidance API"
      DOC "Full path to the ATC3DG.dll file"
      )
  ENDIF(Ascension3DG_MedSafe)

  GET_FILENAME_COMPONENT( ATC_LIBRARY_DIRS ${ATC_LIBRARY} PATH )
  LINK_DIRECTORIES( ${ATC_LIBRARY_DIRS} )
ENDIF(IGSTK_USE_Ascension3DGTracker)

#
# Atracsys Easytrack500
#
OPTION(IGSTK_USE_AtracsysEasyTrack500 "Atracsys EasyTrack500 Tracker" OFF)

IF( IGSTK_USE_AtracsysEasyTrack500 )
  SET(ETK_INCLUDE_DIR CACHE PATH "")
  SET(ETK_LIBRARY_DIR CACHE PATH "" )
  INCLUDE_DIRECTORIES( ${ETK_INCLUDE_DIR} )
  LINK_DIRECTORIES( ${ETK_LIBRARY_DIR} )
ENDIF( IGSTK_USE_AtracsysEasyTrack500 )

# FIND AXIOS3D CAMBAR B2
#
IF(WIN32)
  OPTION(IGSTK_USE_AXIOS3D "Use Axios3D Cambar B2 Tracker" OFF)
  IF(IGSTK_USE_AXIOS3D)
    SET(Axios3D_INCLUDE_DIR CACHE PATH "")
    SET(Axios3D_LIBRARY_DIR CACHE PATH "" )
    INCLUDE_DIRECTORIES( ${Axios3D_INCLUDE_DIR} )
    LINK_DIRECTORIES( ${Axios3D_LIBRARY_DIR} )
  ENDIF(IGSTK_USE_AXIOS3D)
ENDIF(WIN32)

# Connect NDI Certus Tracker
OPTION(IGSTK_USE_NDICertusTracker "Connect NDI Certus Tracker" OFF)
IF(IGSTK_USE_NDICertusTracker)
  # The headers defined in this directory must match the NDI API library
  INCLUDE_DIRECTORIES( ${IGSTK_SOURCE_DIR}/Utilities/NDICertus )

  FIND_LIBRARY( CERTUS_LIBRARY
    NAMES  NDItb NDItb.lib oapi oapi.lib
    PATHS "C:/NDIoapi/ndlib/msvc"
    DOC "Full path to the NDItb.lib (Linux) of oapi.lib (Windows) file"
    )
	
  GET_FILENAME_COMPONENT( CERTUS_LIBRARY_DIRS ${CERTUS_LIBRARY} PATH )
  LINK_DIRECTORIES( ${CERTUS_LIBRARY_DIRS} )
ENDIF(IGSTK_USE_NDICertusTracker)

#
# Use OpenIGTLink protocol
#
OPTION(IGSTK_USE_OpenIGTLink "Use the OpenIGTLink protocol" OFF)
IF(IGSTK_USE_OpenIGTLink)
  #
  #  FIND OpenIGTLink
  #
  find_package(OpenIGTLink)
  IF (OpenIGTLink_USE_FILE)
    INCLUDE (${OpenIGTLink_USE_FILE})
  ELSE (OpenIGTLink_USE_FILE)
    MESSAGE( FATAL_ERROR "This application requires OpenIGTLink. One of these components is missing. Please verify configuration")
  ENDIF (OpenIGTLink_USE_FILE)
ENDIF(IGSTK_USE_OpenIGTLink)

#
# Use OpenCV
#
OPTION(IGSTK_USE_OpenCV "Use the OpenCV library" OFF)
IF(IGSTK_USE_OpenCV)
  #
  #  FIND OpenCV
  find_package( OpenCV REQUIRED )
ENDIF(IGSTK_USE_OpenCV)

# Use ArucoTracker
OPTION(IGSTK_USE_ArucoTracker "Connect ArucoTracker" OFF)
IF( IGSTK_USE_ArucoTracker )
  IF(NOT IGSTK_USE_OpenCV)
    MESSAGE( FATAL_ERROR "In order to use ArucoTracker, OpenCV is required. Turn the IGSTK_USE_OpenCV flag to on.")
  ELSE(NOT IGSTK_USE_OpenCV)
    INCLUDE_DIRECTORIES( ${IGSTK_SOURCE_DIR}/Utilities/Aruco/aruco-1.1.0_SRC )
  ENDIF(NOT IGSTK_USE_OpenCV)
ENDIF( IGSTK_USE_ArucoTracker )

#
# This option enables the scene graph observer to collect relavant events to constuct the scene graph tree
#
OPTION(IGSTK_USE_SceneGraphVisualization "Enable scene graph visualization by allowing the observer to listen to relevant events" ON)
IF(IGSTK_USE_SceneGraphVisualization)
  INCLUDE_DIRECTORIES( ${IGSTK_SOURCE_DIR}/Utilities/SceneGraphVisualization )
ENDIF(IGSTK_USE_SceneGraphVisualization)

#-----------------------------------------------------------------------------
# IGSTK build video imager classes
OPTION(IGSTK_USE_VideoImager "Enable video support" OFF)

#-----------------------------------------------------------------------------
# Configure the default IGSTK_DATA_ROOT for the location of IGSTK Data.
FIND_PATH(IGSTK_DATA_ROOT igstkDataReadMe.txt ${IGSTK_SOURCE_DIR}/Testing/Data $ENV{IGSTK_DATA_ROOT})
MARK_AS_ADVANCED(IGSTK_DATA_ROOT)

#-----------------------------------------------------------------------------
# The entire IGSTK tree should use the same include path, except for the
# Wrapping directory.

# Create the list of include directories needed for IGSTK header files.
INCLUDE(${IGSTK_SOURCE_DIR}/igstkIncludeDirectories.cmake)

# This should be the only INCLUDE_DIRECTORIES command in the entire
# tree, except for the Utilities and Wrapping directories.  We need to
# do this in one place to make sure the order is correct.
INCLUDE_DIRECTORIES(
  ${ITK_DIR}/Utilities/zlib
  ${IGSTK_INCLUDE_DIRS_BUILD_TREE}
  ${IGSTK_INCLUDE_DIRS_BUILD_TREE_CXX}
  ${IGSTK_INCLUDE_DIRS_SYSTEM}
)

#-----------------------------------------------------------------------------
# check for header files that we need
INCLUDE (${CMAKE_ROOT}/Modules/CheckIncludeFile.cmake)
# for serial communication: termio.h or termios.h?
CHECK_INCLUDE_FILE("termios.h"       HAVE_TERMIOS_H)
CHECK_INCLUDE_FILE("termio.h"        HAVE_TERMIO_H)

# Configure a header needed by igstkSystemInformation.
CONFIGURE_FILE("${IGSTK_SOURCE_DIR}/igstkConfigure.h.in"
               "${IGSTK_BINARY_DIR}/igstkConfigure.h")

#-----------------------------------------------------------------------------
# Help other projects use IGSTK.
SET(IGSTK_LIBRARY_PATH ${LIBRARY_OUTPUT_PATH})


# Copy the UseIGSTK.cmake file to the binary tree for backward compatability.
CONFIGURE_FILE(${IGSTK_SOURCE_DIR}/UseIGSTK.cmake.in
               ${IGSTK_BINARY_DIR}/UseIGSTK.cmake COPYONLY IMMEDIATE)

#-----------------------------------------------------------------------------
# Installation vars.
# IGSTK_INSTALL_BIN_DIR          - binary dir (executables)
# IGSTK_INSTALL_LIB_DIR          - library dir (libs)
# IGSTK_INSTALL_DATA_DIR         - share dir (say, examples, data, etc)
# IGSTK_INSTALL_INCLUDE_DIR      - include dir (headers)
# IGSTK_INSTALL_PACKAGE_DIR      - package/export configuration files

IF(NOT IGSTK_INSTALL_BIN_DIR)
   SET(IGSTK_INSTALL_BIN_DIR
      ${CMAKE_INSTALL_PREFIX}/bin
   )
ENDIF(NOT IGSTK_INSTALL_BIN_DIR)

IF(NOT IGSTK_INSTALL_LIB_DIR)
   SET(IGSTK_INSTALL_LIB_DIR
      ${CMAKE_INSTALL_PREFIX}/lib/IGSTK
   )
ENDIF(NOT IGSTK_INSTALL_LIB_DIR)

IF(NOT IGSTK_INSTALL_DATA_DIR)
   SET(IGSTK_INSTALL_DATA_DIR
      ${CMAKE_INSTALL_PREFIX}/share/IGSTK
   )
ENDIF(NOT IGSTK_INSTALL_DATA_DIR)

IF(NOT IGSTK_INSTALL_INCLUDE_DIR)
   SET(IGSTK_INSTALL_INCLUDE_DIR
      ${CMAKE_INSTALL_PREFIX}/include/IGSTK
   )
ENDIF(NOT IGSTK_INSTALL_INCLUDE_DIR)

IF(NOT IGSTK_INSTALL_PACKAGE_DIR)
   SET(IGSTK_INSTALL_PACKAGE_DIR
      ${IGSTK_INSTALL_LIB_DIR}
#      CACHE INTERNAL ""
   )
ENDIF(NOT IGSTK_INSTALL_PACKAGE_DIR)


# Save library dependencies.
EXPORT_LIBRARY_DEPENDENCIES(${IGSTK_BINARY_DIR}/IGSTKLibraryDepends.cmake)

# Create the IGSTKConfig.cmake file containing the IGSTK configuration.
INCLUDE (${IGSTK_SOURCE_DIR}/igstkGenerateIGSTKConfig.cmake)

# Install some files.
INSTALL(FILES
   ${IGSTK_BINARY_DIR}/igstkConfigure.h
   DESTINATION ${IGSTK_INSTALL_INCLUDE_DIR}
)

INSTALL(FILES
   ${IGSTK_BINARY_DIR}/IGSTKBuildSettings.cmake
   ${IGSTK_BINARY_DIR}/IGSTKLibraryDepends.cmake
   ${IGSTK_BINARY_DIR}/UseIGSTK.cmake
   DESTINATION ${IGSTK_INSTALL_PACKAGE_DIR}
)

#-----------------------------------------------------------------------------
# Set up CPack support, to be able to distribute IGSTK
#
INCLUDE(InstallRequiredSystemLibraries)
SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "IGSTK ")
SET(CPACK_PACKAGE_VENDOR "ISC")
SET(CPACK_PACKAGE_VERSION_MAJOR "${IGSTK_VERSION_MAJOR}")
SET(CPACK_PACKAGE_VERSION_MINOR "${IGSTK_VERSION_MINOR}")
SET(CPACK_PACKAGE_VERSION_PATCH "${IGSTK_VERSION_PATCH}")
SET(CPACK_PACKAGE_INSTALL_DIRECTORY "IGSTK-${IGSTK_VERSION_MAJOR}.${IGSTK_VERSION_MINOR}")
INCLUDE(CPack)


#-----------------------------------------------------------------------------
# Add compiler flags IGSTK needs to work on this platform.  This must be
# done AFTER the call to CMAKE_EXPORT_BUILD_SETTINGS.
#-----------------------------------------------------------------------------
# ITK requires special compiler flags on some platforms.
if(CMAKE_COMPILER_IS_GNUCXX)
  set(IGSTK_REQUIRED_C_FLAGS "${IGSTK_REQUIRED_C_FLAGS} -Wall -Wno-uninitialized -Wno-unused-parameter")
  set(IGSTK_REQUIRED_CXX_FLAGS "${IGSTK_REQUIRED_CXX_FLAGS} -ftemplate-depth-50 -Wall")
  include(${IGSTK_SOURCE_DIR}/CMake/itkCheckCXXAcceptsFlags.cmake)
  itkCHECK_CXX_ACCEPTS_FLAGS("-Wno-deprecated" CXX_HAS_DEPRECATED_FLAG)
  if(CXX_HAS_DEPRECATED_FLAG)
    set(IGSTK_REQUIRED_CXX_FLAGS "${IGSTK_REQUIRED_CXX_FLAGS} -Wno-deprecated")
  endif(CXX_HAS_DEPRECATED_FLAG)
endif(CMAKE_COMPILER_IS_GNUCXX)

SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${IGSTK_REQUIRED_C_FLAGS}")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${IGSTK_REQUIRED_CXX_FLAGS}")
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${IGSTK_REQUIRED_LINK_FLAGS}")
SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${IGSTK_REQUIRED_LINK_FLAGS}")
SET(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${IGSTK_REQUIRED_LINK_FLAGS}")

IF(MSVC_VERSION EQUAL 1400 OR MSVC_VERSION GREATER 1400)
  ADD_DEFINITIONS(-D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE -D_CRT_SECURE_NO_WARNINGS)
  ADD_DEFINITIONS(-D_SCL_SECURE_NO_DEPRECATE -D_SCL_SECURE_NO_WARNINGS)
ENDIF(MSVC_VERSION EQUAL 1400 OR MSVC_VERSION GREATER 1400)

SUBDIRS(Utilities)

OPTION(BUILD_DOCUMENTATION "Build IGSTK Documentation" OFF)
IF(BUILD_DOCUMENTATION)
  find_package(VTK)
  IF (USE_VTK_FILE)
    INCLUDE (${USE_VTK_FILE})
  ELSE (USE_VTK_FILE)
    MESSAGE( FATAL_ERROR "This application requires VTK. One of these components is missing. Please verify configuration")
  ENDIF (USE_VTK_FILE)
  SUBDIRS(Utilities/Doxygen)
ENDIF(BUILD_DOCUMENTATION)

#-----------------------------------------------------------------------------
# IGSTK uses KWStyle for checking the coding style
OPTION(IGSTK_USE_KWSTYLE "Run KWStyle in order to check for violations of the coding standard." OFF)
MARK_AS_ADVANCED(IGSTK_USE_KWSTYLE)

IF(IGSTK_USE_KWSTYLE)
  FIND_PROGRAM(KWSTYLE_EXECUTABLE
  NAMES KWStyle
  PATHS
  /usr/local/bin
  )
  MARK_AS_ADVANCED(KWSTYLE_EXECUTABLE)

  OPTION(KWSTYLE_USE_VIM_FORMAT "Set KWStyle to generate errors with a VIM-compatible format." OFF)
  OPTION(KWSTYLE_USE_MSVC_FORMAT "Set KWStyle to generate errors with a VisualStudio-compatible format." OFF)

  CONFIGURE_FILE(${IGSTK_SOURCE_DIR}/Utilities/KWStyle/IGSTK.kws.xml.in
               ${IGSTK_BINARY_DIR}/Utilities/KWStyle/IGSTK.kws.xml)

  CONFIGURE_FILE(${IGSTK_SOURCE_DIR}/Utilities/KWStyle/IGSTKFiles.txt.in
               ${IGSTK_BINARY_DIR}/Utilities/KWStyle/IGSTKFiles.txt)

  SET(KWSTYLE_ARGUMENTS -xml ${IGSTK_BINARY_DIR}/Utilities/KWStyle/IGSTK.kws.xml
                        -o ${IGSTK_SOURCE_DIR}/Utilities/KWStyle/IGSTKOverwrite.txt
                        -v
                        -D ${IGSTK_BINARY_DIR}/Utilities/KWStyle/IGSTKFiles.txt
  )

  IF(KWSTYLE_USE_VIM_FORMAT)
    SET(KWSTYLE_ARGUMENTS -vim ${KWSTYLE_ARGUMENTS})
  ENDIF(KWSTYLE_USE_VIM_FORMAT)

  IF(KWSTYLE_USE_MSVC_FORMAT)
    SET(KWSTYLE_ARGUMENTS -msvc ${KWSTYLE_ARGUMENTS})
  ENDIF(KWSTYLE_USE_MSVC_FORMAT)

    ADD_CUSTOM_COMMAND(
    OUTPUT  ${IGSTK_BINARY_DIR}/KWStyleReport.txt
    COMMAND ${KWSTYLE_EXECUTABLE}
    ARGS    ${KWSTYLE_ARGUMENTS}
    COMMENT "Coding Style Checker"
    )
  ADD_CUSTOM_TARGET(StyleCheck DEPENDS ${IGSTK_BINARY_DIR}/KWStyleReport.txt)
ENDIF(IGSTK_USE_KWSTYLE)

# Microsoft Visual Studio 10 needs additional flag in order to compile in
# 64 Bit mode
IF(CMAKE_GENERATOR STREQUAL "Visual Studio 10 Win64")
  SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /bigobj")
ENDIF(CMAKE_GENERATOR STREQUAL "Visual Studio 10 Win64")

IF( WIN32 )
  SET( IGSTK_SERIAL_PORT_0 "COM1:" CACHE STRING "First serial port device name (Default COM1)")
  SET( IGSTK_SERIAL_PORT_1 "COM2:" CACHE STRING "Second serial port device name (Default COM2)")
  SET( IGSTK_SERIAL_PORT_2 "COM3:" CACHE STRING "Third serial port device name (Default COM3)")
  SET( IGSTK_SERIAL_PORT_3 "COM4:" CACHE STRING "Fourth serial port device name (Default COM4)")
  SET( IGSTK_SERIAL_PORT_4 "COM5:" CACHE STRING "Fifith serial port device name (Default COM5)")
  SET( IGSTK_SERIAL_PORT_5 "COM6:" CACHE STRING "Sixth serial port device name (Default COM6)")
  SET( IGSTK_SERIAL_PORT_6 "COM7:" CACHE STRING "Seventh serial port device name (Default COM7)")
  SET( IGSTK_SERIAL_PORT_7 "COM8:" CACHE STRING "Eighth serial port device name (Default COM7)")
ELSE (WIN32 )
  SET( IGSTK_SERIAL_PORT_0 "/dev/ttyS0" CACHE STRING "First serial port device name  (Default /dev/ttyS0)")
  SET( IGSTK_SERIAL_PORT_1 "/dev/ttyS1" CACHE STRING "Second serial port device name  (Default /dev/ttyS1)")
  SET( IGSTK_SERIAL_PORT_2 "/dev/ttyS2" CACHE STRING "Third serial port device name  (Default /dev/ttyS2)")
  SET( IGSTK_SERIAL_PORT_3 "/dev/ttyS3" CACHE STRING "Fourth serial port device name  (Default /dev/ttyS3)")
  SET( IGSTK_SERIAL_PORT_4 "/dev/ttyS4" CACHE STRING "Fifith serial port device name  (Default /dev/ttyS4)")
  SET( IGSTK_SERIAL_PORT_5 "/dev/ttyS5" CACHE STRING "Sixth serial port device name  (Default /dev/ttyS5)")
  SET( IGSTK_SERIAL_PORT_6 "/dev/ttyS6" CACHE STRING "Seventh serial port device name  (Default /dev/ttyS6)")
  SET( IGSTK_SERIAL_PORT_7 "/dev/ttyS7" CACHE STRING "Eighth serial port device name  (Default /dev/ttyS7)")
ENDIF ( WIN32 )
#----------------------------------------------------------------------------
configure_file(CMake/CTestCustom.cmake.in CTestCustom.cmake @ONLY)
